package com.caigou.modules.tools.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.tools.bean.ActiveBean
import com.caigou.modules.tools.bean.CardBean
import com.caigou.modules.tools.bean.RedPackageBean
import com.caigou.modules.tools.bean.UserSignInBean
import com.caigou.modules.tools.repository.ToolsRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/10/28
 */
@HiltViewModel
class ToolsViewModel @Inject constructor(private val repository: ToolsRepository) : ViewModel() {

    val cardListLiveData = MutableLiveData<Resource<CardBean>>()
    val activeLiveData = MutableLiveData<Resource<ActiveBean>>()
    val userSignInLiveData = MutableLiveData<Resource<UserSignInBean>>()
    val redPackageLiveData = MutableLiveData<Resource<RedPackageBean>>()
    val renameCardLiveData = MutableLiveData<Resource<String>>()
    val deleteCardLiveData = MutableLiveData<Resource<String>>()
    val buyFlowerLiveData = MutableLiveData<Resource<String>>()

    fun getCardList(userId: String) {
        cardListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCardList(userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<CardBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    cardListLiveData.value = it
                }
        }
    }

    fun getActiveInfo(userId: String) {
        activeLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getActiveInfo(userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<ActiveBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    activeLiveData.value = it
                }
        }
    }

    fun setUserSignIn(userId: String) {
        userSignInLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setUserSignIn(userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<UserSignInBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    userSignInLiveData.value = it
                }
        }
    }

    fun getRedPackage(userId: String, taskId: Int) {
        redPackageLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getRedPackage(userId, taskId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<RedPackageBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    redPackageLiveData.value = it
                }
        }
    }

    fun exchangeRenameCard(userId: String) {
        renameCardLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.exchangeRenameCard(userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    renameCardLiveData.value = it
                }
        }
    }

    fun exchangeDeleteCard(userId: String) {
        deleteCardLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.exchangeDeleteCard(userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    deleteCardLiveData.value = it
                }
        }
    }

    fun buyFlower(userId: String, number: Int) {
        buyFlowerLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.buyFlower(userId, number)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    buyFlowerLiveData.value = it
                }
        }
    }
}